package logic

import (
	"context"
	"group/common"
	"group/internal/svc"
	"group/models"
	"group/pb/group"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGroupLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGroupLogic {
	return &CreateGroupLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CreateGroup 创建群聊
func (l *CreateGroupLogic) CreateGroup(in *group.GroupInfo) (*group.Empty, error) {
	//result, err := l.svcCtx.ChatGroupModel.Insert(l.ctx, &models.ChatGroup{
	//	Name:     in.Name,
	//	UserId:   in.UserId,
	//	Avatar:   in.Avatar,
	//	Desc:     in.Desc,
	//	JoinType: in.JoinType,
	//	Status:   common.GroupStatusNormal,
	//})
	//if err != nil {
	//	return nil, err
	//}
	//
	//lastID, err := result.LastInsertId()
	//if err != nil {
	//	return nil, err
	//}
	//
	//in.Id = lastID
	//l.Logger.Infof("add group: %v", in)

	err := l.svcCtx.ChatGroupAggregationModel.CreateGroup(l.ctx, &models.ChatGroup{
		Name:     in.Name,
		UserId:   in.UserId,
		Avatar:   in.Avatar,
		Desc:     in.Desc,
		JoinType: in.JoinType,
		Status:   common.GroupStatusNormal,
	})
	if err != nil {
		return nil, err
	}
	return &group.Empty{}, nil
}
